//
//  TFMyLotteryViewController.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/22.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFMyLotteryViewController: UIViewController {
    @objc public let winnerLab = UILabel()
    let bg_imgview = UIImageView()
    let lightIMg = UIImageView()
    let closeBtn = UIButton()
    let subLab = UILabel()
    let titleLab = UILabel()
    let leftUpBtn = UIButton()
    let leftbottomBtn = UIButton()
    let rightbottomBtn = UIButton()
    //中奖名单列表
    @objc public let winnerIMg = UIImageView()
    @objc public let nowinnerIMg = UIImageView()
    @objc public let winnerBtn = UIButton()
    @objc public let nowinnerBtn = UIButton()
    @objc public weak var delegate : TFMyLotteryViewControllerDelegate?
    @objc public var isLottery:Bool = false //  是否中奖

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor(red: 5/255.0, green: 0/255.0, blue: 1/255.0, alpha: 0.8)
       
    }
    @objc public func createwinnerView()

    {

        let bg_imgview = self.bg_imgview
        
        if (UIScreen.main.bounds.width > UIScreen.main.bounds.height) {
            
           
            if (UIScreen.main.bounds.height < 375) {
                bg_imgview.frame = CGRect(x: (self.view.frame.size.width-224)/2, y: (self.view.frame.size.height-280)/2.0, width: 224, height: 280)
               
            } else {
                bg_imgview.frame = CGRect(x: (self.view.frame.size.width-256)/2, y: (self.view.frame.size.height-320)/2.0, width: 256, height: 320)
               
            }
        } else {
            
            if (UIScreen.main.bounds.width < 375) {
                bg_imgview.frame = CGRect(x: (self.view.frame.size.width-280)/2, y: (self.view.frame.size.height-350)/2.0, width: 280, height: 350)
             
            } else {
                bg_imgview.frame = CGRect(x: (self.view.frame.size.width-297)/2, y: (self.view.frame.size.height-367)/2.0, width: 297, height: 367)
                
            }
           
            
           
        }
        
        bg_imgview.image = UIImage.init(named: "HTLive.bundle/win_bg")
        bg_imgview.isUserInteractionEnabled = true
        self.view.addSubview(bg_imgview)
        
        let lightIMg = self.lightIMg;
        lightIMg.frame = CGRect(x: 0, y: 0, width: bg_imgview.bounds.size.width, height: bg_imgview.bounds.size.height/2)
      
        lightIMg.image = UIImage.init(named: "HTLive.bundle/light")
        bg_imgview.addSubview(lightIMg)
        
        let closeBtn = self.closeBtn
        closeBtn.frame = CGRect(x: bg_imgview.frame.size.width-33, y: 0, width: 33, height: 33)
        closeBtn.setImage(UIImage.init(named: "HTLive.bundle/win_close"), for: .normal)
        closeBtn.addTarget(self, action: #selector(closeBtnClick), for: .touchUpInside)
        bg_imgview.addSubview(closeBtn)
    
        
        let titleLab = self.titleLab
        titleLab.frame = CGRect(x: 0, y: bg_imgview.frame.size.height*0.25, width: bg_imgview.frame.size.width, height: 30)
  
        titleLab.textAlignment = .center
        titleLab.font = UIFont.PingFang(size: 30)
        titleLab.textColor = UIColor(red: 255/255.0, green: 229/255.0, blue: 161/255.0, alpha: 1)
        //恭喜你中奖啦
        titleLab.text = "恭喜你中奖啦"
        bg_imgview.addSubview(titleLab)
       
        let subLab = self.subLab
         subLab.frame = CGRect(x: 0, y: titleLab.frame.maxY+15, width: bg_imgview.frame.size.width, height: 30)
        subLab.textAlignment = .center
        subLab.font = UIFont.PingFang(size: 13.3)
        subLab.textColor = UIColor(red: 255/255.0, green: 229/255.0, blue: 161/255.0, alpha: 1)
        subLab.text = "请在直播结束后与工作人员联系哦";
        bg_imgview.addSubview(subLab)
        
        
        if (UIScreen.main.bounds.width > UIScreen.main.bounds.height) {
            if (UIScreen.main.bounds.height < 375) {
                self.winnerIMg.frame = CGRect(x: (bg_imgview.frame.size.width-194)/2.0, y: subLab.frame.maxY+17, width: 194, height: 60)
            } else {
                self.winnerIMg.frame = CGRect(x: (bg_imgview.frame.size.width-226)/2.0 , y: subLab.frame.maxY+17, width: 226, height: 67)
            }
            
        } else {
            self.winnerIMg.frame = CGRect(x: (bg_imgview.frame.size.width-254)/2.0, y: subLab.frame.maxY+17, width: 254, height: 78)
    
        }
                       
        self.winnerIMg.image = UIImage.init(named: "HTLive.bundle/win_gift")
  
        bg_imgview.addSubview(self.winnerIMg)
        
    
        self.winnerLab.frame = CGRect(x: 0, y: (winnerIMg.frame.size.height-20)/2.0, width: winnerIMg.frame.size.width, height: 20)
        self.winnerLab.textAlignment = .center
        self.winnerLab.font = UIFont.PingFang(size: 14)
        self.winnerLab.textColor = UIColor(red: 174/255.0, green: 63/255.0, blue: 57/255.0, alpha: 1)
        self.winnerLab.text = "222222";
        winnerIMg.addSubview(self.winnerLab)
        
        
        self.winnerBtn.frame = CGRect(x: (bg_imgview.frame.size.width-110)/2, y: bg_imgview.frame.size.height-30-14, width: 110, height: 14)
        winnerBtn.setTitle("查看中奖名单", for: .normal)
        self.winnerBtn.setImage(UIImage.init(named: "HTLive.bundle/win_go"), for: .normal)
        self.winnerBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -self.winnerBtn.imageView!.frame.size.width, bottom: 0, right: self.winnerBtn.imageView!.frame.size.width)
        self.winnerBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: self.winnerBtn.titleLabel!.frame.size.width, bottom: 0, right: -self.winnerBtn.titleLabel!.frame.size.width)
        self.winnerBtn.setTitleColor(UIColor(red: 255/255.0, green: 229/255.0, blue: 161/255.0, alpha: 1), for: .normal)
        
        self.winnerBtn.titleLabel!.font = UIFont.PingFang(size: 14)
        self.winnerBtn.addTarget(self, action: #selector(winnerBtnClick), for: .touchUpInside)
        
        bg_imgview.addSubview(self.winnerBtn)
      
    }
    @objc public func updateFrame()
    {
        
        if (self.isLottery) { // 中奖
            if (UIScreen.main.bounds.width > UIScreen.main.bounds.height) {
                
               
                if (UIScreen.main.bounds.height < 375) {
                    self.bg_imgview.frame = CGRect(x: (self.view.frame.size.width-224)/2, y: (self.view.frame.size.height-280)/2.0, width: 224, height: 280)
                } else {
                    self.bg_imgview.frame = CGRect(x: (self.view.frame.size.width-256)/2, y: (self.view.frame.size.height-320)/2.0, width: 256, height: 320)
                }
            } else {
                
                if (UIScreen.main.bounds.width < 375) {
                    self.bg_imgview.frame = CGRect(x: (self.view.frame.size.width-280)/2, y: (self.view.frame.size.height-350)/2.0, width: 280, height: 350)
                } else {
                    self.bg_imgview.frame = CGRect(x: (self.view.frame.size.width-297)/2, y: (self.view.frame.size.height-367)/2.0, width: 297, height: 367)
                }
               
                
               
            }
            self.lightIMg.frame = CGRect(x: 0, y: 0, width: self.bg_imgview.bounds.size.width, height: self.bg_imgview.bounds.size.height/2)
        
            self.closeBtn.frame = CGRect(x: self.bg_imgview.frame.size.width-33, y: 0, width: 33, height: 33)
        
            self.titleLab.frame = CGRect(x: 0, y: self.bg_imgview.frame.size.height*0.25, width: self.bg_imgview.frame.size.width, height: 30)
          
            self.subLab.frame = CGRect(x: 0, y: self.titleLab.frame.maxY+15, width: self.bg_imgview.frame.size.width, height: 30)
            

            
            if (UIScreen.main.bounds.width > UIScreen.main.bounds.height) {
                if (UIScreen.main.bounds.height < 375) {
                    self.winnerIMg.frame = CGRect(x: (self.bg_imgview.frame.size.width-194)/2.0, y: self.subLab.frame.maxY+17, width: 194, height: 60)
                } else {
                    self.winnerIMg.frame = CGRect(x: (self.bg_imgview.frame.size.width-226)/2.0, y: self.subLab.frame.maxY+17, width: 226, height: 67)
                
                }
                
            } else {
                self.winnerIMg.frame = CGRect(x: (self.bg_imgview.frame.size.width-254)/2.0, y: self.subLab.frame.maxY+17, width: 254, height: 78) //CGRectMake(, CGRectGetMaxY(self.subLab.frame)+17, 254, 78);
            }
            self.winnerLab.frame = CGRect(x: 0, y: (self.winnerIMg.frame.size.height-20)/2.0, width: self.winnerIMg.frame.size.width, height: 20)
        
            self.winnerBtn.frame = CGRect(x: (self.bg_imgview.frame.size.width-110)/2, y: self.bg_imgview.frame.size.height-30-14, width: 110, height: 14)
            
            self.winnerBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -self.winnerBtn.imageView!.frame.size.width, bottom: 0, right: self.winnerBtn.imageView!.frame.size.width)
            self.winnerBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: self.winnerBtn.titleLabel!.frame.size.width, bottom: 0, right: -self.winnerBtn.titleLabel!.frame.size.width)
          
       
        } else {
            if (UIScreen.main.bounds.width > UIScreen.main.bounds.height) {
                
               
                if (UIScreen.main.bounds.height < 375) {
                    self.bg_imgview.frame = CGRect(x: (self.view.frame.size.width-224)/2, y: (self.view.frame.size.height-280)/2.0, width: 224, height: 280)
               
                } else {
                    self.bg_imgview.frame = CGRect(x: (self.view.frame.size.width-256)/2, y: (self.view.frame.size.height-320)/2.0, width: 256, height: 320)
                }
            } else {
                
                if (UIScreen.main.bounds.height < 375) {
                    self.bg_imgview.frame = CGRect(x: (self.view.frame.size.width-280)/2, y: (self.view.frame.size.height-350)/2.0, width: 280, height: 350)
                    
                } else {
                    self.bg_imgview.frame = CGRect(x: (self.view.frame.size.width-297)/2, y: (self.view.frame.size.height-367)/2.0, width: 297, height: 367)
                    
                
                }
               
                
               
            }
            self.leftUpBtn.frame = CGRect(x: 0, y: 0, width: 22, height: 22)
            
            self.leftbottomBtn.frame = CGRect(x: 0, y: self.bg_imgview.frame.size.height-22, width: 22, height: 22)
            
      
        
            self.rightbottomBtn.frame = CGRect(x: self.bg_imgview.frame.size.width-22, y: self.bg_imgview.frame.size.height-22, width: 22, height: 22)
            
            
            self.closeBtn.frame = CGRect(x: self.bg_imgview.frame.size.width-33, y: 0, width: 33, height: 33)
            
            self.titleLab.frame = CGRect(x: 0, y: self.bg_imgview.frame.size.height*0.25, width: self.bg_imgview.frame.size.width, height: 30)
   
            
            self.subLab.frame = CGRect(x: 0, y: self.titleLab.frame.maxY+15, width: self.bg_imgview.frame.size.width, height: 30)
        
            
            if (UIScreen.main.bounds.width > UIScreen.main.bounds.height) {
                if (UIScreen.main.bounds.height < 375) {
                    self.nowinnerIMg.frame = CGRect(x: (self.bg_imgview.frame.size.width-194)/2.0, y: self.subLab.frame.maxY+17, width: 194, height: 60)
                } else {
                    self.nowinnerIMg.frame = CGRect(x: (self.bg_imgview.frame.size.width-226)/2.0, y: self.subLab.frame.maxY+17, width: 226, height: 67)
                }
                
            } else {
                
                self.nowinnerIMg.frame = CGRect(x: (self.bg_imgview.frame.size.width-254)/2.0, y: self.subLab.frame.maxY+17, width: 254, height: 78)
            }
            self.winnerLab.frame = CGRect(x: 0, y: (self.nowinnerIMg.frame.size.height-20)/2.0, width: self.nowinnerIMg.frame.size.width, height: 20)
            
         
            
            self.nowinnerBtn.frame = CGRect(x: (self.bg_imgview.frame.size.width-110)/2, y: self.bg_imgview.frame.size.height-30-14, width: 110, height: 14)
            
            self.nowinnerBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -self.nowinnerBtn.imageView!.frame.size.width, bottom: 0, right: self.nowinnerBtn.imageView!.frame.size.width)
            self.nowinnerBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: self.nowinnerBtn.titleLabel!.frame.size.width, bottom: 0, right: -self.nowinnerBtn.titleLabel!.frame.size.width)
           
        }
    }
    @objc func closeBtnClick()
    {
        UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
            self.view.alpha = 0.0;
        } completion: { Bool in
            self.view.removeFromSuperview()
        }
    
    }
    
    @objc func createNOwinnerView()
    {

        let bg_imgview = UIImageView()
        
        if (UIScreen.main.bounds.width > UIScreen.main.bounds.height) {
            
           
            if (UIScreen.main.bounds.height < 375) {
                bg_imgview.frame = CGRect(x: (self.view.frame.size.width-224)/2, y: (self.view.frame.size.height-280)/2.0, width: 224, height: 280)
                
            } else {
                bg_imgview.frame = CGRect(x: (self.view.frame.size.width-256)/2, y: (self.view.frame.size.height-320)/2.0, width: 256, height: 320)
            }
        } else {
            
            if (UIScreen.main.bounds.width < 375) {
                bg_imgview.frame = CGRect(x: (self.view.frame.size.width-280)/2, y: (self.view.frame.size.height-350)/2.0, width: 280, height: 350)
            
            } else {
                bg_imgview.frame = CGRect(x: (self.view.frame.size.width-297)/2, y: (self.view.frame.size.height-367)/2.0, width: 297, height: 367)
            
            }
           
            
           
        }
        
        
        bg_imgview.image = UIImage.init(named: "HTLive.bundle/list_bg")
        bg_imgview.isUserInteractionEnabled = true
        self.view.addSubview(bg_imgview)
        
        let leftUpBtn = self.leftUpBtn
        leftUpBtn.frame = CGRect(x: 0, y: 0, width: 22, height: 22)
        leftUpBtn.setImage(UIImage.init(named: "HTLive.bundle/notwin_angle_01"), for: .normal)
        bg_imgview.addSubview(leftUpBtn)
    
        
        let leftbottomBtn = self.leftbottomBtn
        leftbottomBtn.frame = CGRect(x: 0, y: bg_imgview.frame.size.height-22, width: 22, height: 22)
        leftbottomBtn.setImage(UIImage.init(named: "HTLive.bundle/notwin_angle_02"), for: .normal)
        bg_imgview.addSubview(leftbottomBtn)

        
        let rightbottomBtn = self.rightbottomBtn
        rightbottomBtn.frame = CGRect(x: bg_imgview.frame.size.width-22, y: bg_imgview.frame.size.height-22, width: 22, height: 22)
        rightbottomBtn.setImage(UIImage.init(named: "HTLive.bundle/notwin_angle_03"), for: .normal)
        bg_imgview.addSubview(rightbottomBtn)
        
        
        let closeBtn = UIButton()
        closeBtn.frame = CGRect(x: bg_imgview.frame.size.width-33, y: 0, width: 33, height: 33)
        closeBtn.setImage(UIImage.init(named: "HTLive.bundle/nowin_close"), for: .normal)
        closeBtn.addTarget(self, action:#selector(closeBtnClick), for: .touchUpInside)
        bg_imgview.addSubview(closeBtn)
        
        let titleLab = UILabel.init(frame: CGRect(x: 0, y: bg_imgview.frame.size.height*0.25, width: bg_imgview.frame.size.width, height: 30))
        titleLab.textAlignment = .center;
        titleLab.font = UIFont.PingFang(size: 20)
        titleLab.textColor = UIColor(red: 102/255.0, green: 31/255.0, blue: 13/255.0, alpha: 1)
        titleLab.text = "很遗憾你没中奖"
        bg_imgview.addSubview(titleLab)
        
        
        let subLab = UILabel.init(frame: CGRect(x: 0, y: titleLab.frame.maxY+15, width: bg_imgview.frame.size.width, height: 30))
        subLab.textAlignment = .center;
        subLab.font = UIFont.PingFang(size: 13.3)
        subLab.textColor = UIColor(red: 102/255.0, green: 31/255.0, blue: 13/255.0, alpha: 1)
        subLab.text = "积攒运气下次再战";
        bg_imgview.addSubview(subLab)
        
        
        let  winnerIMg = self.nowinnerIMg
        
        if (UIScreen.main.bounds.width > UIScreen.main.bounds.height) {
            if (UIScreen.main.bounds.height < 375) {
                winnerIMg.frame = CGRect(x: (bg_imgview.frame.size.width-194)/2.0, y: subLab.frame.maxY+17, width: 194, height: 60)
            } else {
                winnerIMg.frame = CGRect(x: (bg_imgview.frame.size.width-226)/2.0, y: subLab.frame.maxY+17, width: 226, height: 67)
            }
            
        } else {
            
            winnerIMg.frame = CGRect(x: (bg_imgview.frame.size.width-254)/2.0, y: subLab.frame.maxY+17, width: 254, height: 78)
           
        }
        
        winnerIMg.image = UIImage.init(named: "HTLive.bundle/notwin_gift")
       
        bg_imgview.addSubview(winnerIMg)
        
        self.winnerLab.frame = CGRect(x: 0, y: (winnerIMg.frame.size.height-20)/2.0, width: winnerIMg.frame.size.width, height: 20)
    
        self.winnerLab.textAlignment = .center
        self.winnerLab.font = UIFont.PingFang(size: 14)
        self.winnerLab.textColor = UIColor(red: 241/255.0, green: 97/255.0, blue: 60/255.0, alpha: 1)
        self.winnerLab.text = "222222";
        winnerIMg.addSubview(self.winnerLab)
        
        
        let winnerBtn = self.nowinnerBtn
        
        winnerBtn.frame = CGRect(x: (bg_imgview.frame.size.width-110)/2, y: bg_imgview.frame.size.height-30-14, width: 110, height: 14)
        winnerBtn.setTitle("查看中奖名单", for: .normal)
        winnerBtn.setImage(UIImage.init(named: "HTLive.bundle/notwin_more"), for: .normal)
        winnerBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -winnerBtn.imageView!.frame.size.width, bottom: 0, right: winnerBtn.imageView!.frame.size.width)
        winnerBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: winnerBtn.titleLabel!.frame.size.width, bottom: 0, right: -winnerBtn.titleLabel!.frame.size.width)
        winnerBtn.setTitleColor(UIColor(red: 102/255.0, green: 31/255.0, blue: 13/255.0, alpha: 1), for: .normal)
        
        winnerBtn.titleLabel!.font = UIFont.PingFang(size: 14)
        winnerBtn.addTarget(self, action: #selector(winnerBtnClick), for: .touchUpInside)
        bg_imgview.addSubview(winnerBtn)
        
      
    }
    
    @objc func winnerBtnClick()

    {

        
               if self.delegate != nil && (self.delegate?.responds(to: #selector(TFMyLotteryViewControllerDelegate.getlottoryList)))!{
                   
                  
                   self.delegate?.getlottoryList()
               }
    
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.alpha = 1.0;
    }
    
 
   
}


@objc protocol TFMyLotteryViewControllerDelegate : NSObjectProtocol {
    //全屏图片                                图片序列号
    func getlottoryList()
}
